package com.thread.www;

/**
 * @author 新技术软件：汪洋
 */
public class ThreadInterruptDemo implements Runnable {
    public static void main(String[] args)throws Exception{
        Thread thread = new Thread(new ThreadInterruptDemo() , "interruptDemo Thread");
        System.out.println("starting thread ...");
        thread.start();
        thread.sleep(1000);
        System.out.println("interrupting thread...");
        thread.interrupt();
        System.out.println("线程是否终端 ： " + thread.isInterrupted());
        thread.sleep(1000);
        System.out.println("stopping application");
    }

    public void run(){
        boolean stop = false;
        while(!stop){
            System.out.println("my thread is running");
            try{
                Thread.sleep(3l);
            }catch(InterruptedException e){
                System.out.println("break1");
                break;
            }
            long time = System.currentTimeMillis();
            while(System.currentTimeMillis() - time < 1000){

            }
            if(Thread.currentThread().isInterrupted()){
                System.out.println("break2");
                break;
            }
        }
        System.out.println("my thread exiting under request");
    }
}
